package com.example.autopia.view.Activities

import android.app.Dialog
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.Button
import android.widget.ImageButton
import android.widget.TextView
import com.example.autopia.R
import com.example.autopia.databinding.ActivityParkingListBinding
import com.example.autopia.model.Item.Parking
import com.example.autopia.model.Repositories.ParkingRepository
import com.example.autopia.model.Repositories.TestRepository
import com.example.autopia.view.Adapters.ParkingItemAdapter
import com.example.autopia.view.Listeners.ParkingItemListener

class ParkingListActivity : AppCompatActivity() {
    private lateinit var binding: ActivityParkingListBinding
    private val repository = ParkingRepository()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityParkingListBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.btBack.setOnClickListener {
            onBackPressed()
        }
        binding.btInfo.setOnClickListener {
            showDialogInfo()
        }
        val adapter = ParkingItemAdapter()
        binding.recyclerView.adapter = adapter
        adapter.setData(TestRepository.getParkingList())

        val list = repository.getAllItems()
        val parkings = ArrayList<Parking>()
        list.observe(this){
            parkings.addAll(it)
            adapter.setData(parkings)
            binding.recyclerView.visibility = View.VISIBLE
            binding.loading.visibility = View.GONE
        }


        adapter.attachListener(object : ParkingItemListener{
            override fun onInfoClicked(parking: Parking) {
                showDialog(parking)
            }

            override fun onItemClicked(parking: Parking) {
                val intent = Intent(this@ParkingListActivity, ParkingActivity::class.java)
                intent.putExtra("parkingId", parking.id)
                startActivity(intent)
            }
        })
    }

    private fun showDialog(parking: Parking) {
        val dialog = Dialog(this@ParkingListActivity)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.setContentView(R.layout.dialog_parking_info)
        val btPark = dialog.findViewById<Button>(R.id.btPark)
        val btClose = dialog.findViewById<ImageButton>(R.id.btClose)
        val tvName = dialog.findViewById<TextView>(R.id.tvName)
        val tvDescription = dialog.findViewById<TextView>(R.id.tvDescription)
        val tvCountPlace = dialog.findViewById<TextView>(R.id.tvCountPlace)
        val tvPrice = dialog.findViewById<TextView>(R.id.tvPrice)
        tvName.text = parking.name
        tvDescription.text = parking.description
        tvCountPlace.text = parking.freePlaces.toString() + " из " + parking.allPlaces.toString() + " машин"
        tvPrice.text = parking.price.toString() + " руб/час"
        btPark.setOnClickListener {
            val intent = Intent(this, ParkingActivity::class.java)
            intent.putExtra("parkingId", parking.id)
            startActivity(intent)
        }
        btClose.setOnClickListener {
            dialog.dismiss()
        }
        dialog.show()
        dialog.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog.window?.attributes!!.windowAnimations = R.style.DialogAnimation
        dialog.window?.setGravity(Gravity.BOTTOM)
    }

    private fun showDialogInfo() {
        val dialog = Dialog(this@ParkingListActivity)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.setContentView(R.layout.dialog_about_parking_list)
        val btClose = dialog.findViewById<ImageButton>(R.id.btClose)

        btClose.setOnClickListener {
            dialog.dismiss()
        }
        dialog.show()
        dialog.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog.window?.attributes!!.windowAnimations = R.style.DialogAnimation
        dialog.window?.setGravity(Gravity.BOTTOM)
    }
}